<?php
/**
 * @ClassName: Customermaster
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-13 18:20
 * @Version: V1.0.0
 */

namespace app\admin\controller\customer;

use app\common\model;
use think\Controller;
use tpext\builder\traits\HasBuilder;
use function input;
use function request;
use function url;

class Customermaster extends Controller
{
    use HasBuilder;

    protected $dataModel;
    protected $customer_id;

    protected function initialize()
    {
        $this->dataModel = new model\CustomerMaster();

        $this->pageTitle = '送水师傅管理';
        $this->sortOrder = 'id desc';
        $this->pagesize = 10;
        $this->pk = 'id';

        $this->indexWith = ['customer', 'master', 'store'];
    }

    /**
     * 构建搜索
     * @return void
     */
    protected function buildSearch()
    {
        $search = $this->search;
    }

    /**
     * 构建查询条件
     * @return array
     */
    protected function filterWhere()
    {
        $searchData = request()->isGet() ? request()->param() : request()->post();
        $this->customer_id = input('customer_id', 0);

        $where = [];
        if (!empty($this->customer_id)) {
            $where[] = ['customer_id', 'eq', $this->customer_id];
        }

        return $where;
    }

    /**
     * 构建表格
     * @param $data
     * @return void
     */
    protected function buildTable(&$data = [])
    {
        $table = $this->table;

        $table->show('id', 'ID');
        $table->show('store_id', "水站")->to('{store_id}#{store.store_name}');
        $table->show('master_id', '送水师傅')->to('{master_id}#{master.nickname}（{master.mobile}）');
        $table->show('store_phone', "水站电话")->to('{store.store_phone}');
        $table->show('store_adress', "水站地址")->to('{store.store_adress}');

        $table->fields('create_times', '分配/更新时间')->with(
            $table->show('create_time', '分配时间'),
            $table->show('update_time', '更新时间')
        )->getWrapper()->addStyle('width:160px');

        $table->sortable('id, create_time');

        $table->getToolbar()
            ->btnAdd(url('add', ['customer_id' => $this->customer_id]))
            ->btnRefresh();

        $table->getActionbar()
            ->btnEdit(url('edit', ['id' => '__data.pk__', 'customer_id' => $this->customer_id]));
    }

    /**
     * 构建表单
     * @param $isEdit
     * @param $data
     * @return void
     */
    protected function buildForm($isEdit, &$data = [])
    {
        $form = $this->form;

        $data['customer_id'] = input('customer_id', '');

        $store_list = model\Store::where(['id' => $data['store_id'] ?? 0])->field('id,store_name')->select();
        $master_list = model\Member::where(['id' => $data['master_id'] ?? 0])->field('id,nickname,mobile')->select();

        $form->hidden('id');
        $form->hidden('customer_id');
        if ($isEdit) {
            $form->hidden('store_id');
            $form->match('store_id', '水站')->to('{store.id}#{store.store_name}');
            $form->select('master_id', '师傅')->optionsData($master_list, 'nickname')->dataUrl(url('admin/member/selectMemberPage?prev_val=' . $data['store_id'] . '&type=4'), 'nickname');
        } else {
            $form->select('store_id', '水站')->size(0, 12)->optionsData($store_list, 'store_name')->dataUrl(url('admin/store/selectStorePage?type=1'), 'store_name')->readonly($isEdit)->withNext(
                $form->select('master_id', '师傅')->size(0, 12)->optionsData($master_list, 'nickname')->dataUrl(url('admin/member/selectMemberPage?type=4'), 'nickname')
            );
        }
    }

    /**
     * 保存
     * @param $id
     * @return \think\response\Json|\tpext\think\View
     */
    private function save($id = 0)
    {
        $data = request()->post();
        $result = $this->validate($data, []);

        if (!$result) {
            $this->error($result);
        }
//        var_dump($data);

//        return $this->builder()->layer()->closeRefresh(1, __blang('bilder_save_succeeded'));
        return $this->doSave($data, $id);
    }

}